package com.jbh.wanandroidkotlin.util

import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.os.Build
import android.view.ContextThemeWrapper
import android.view.View
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import androidx.appcompat.app.AppCompatActivity



const val USER_INFO = "user_info"
const val ADMIN = "admin"
const val CHAPTER_TOPS="chapterTops"
const val COIN_COUNT = "coinCount"
const val COLLECT_ID = "collectIds"
const val EMAIL="email"
const val ICON = "icon"
const val ID = "id"
const val NICKNAME="nickname"
const val PASSWORD = "password"
const val PUBLIC_NAME = "publicName"
const val TOKEN ="token"
const val TYPE="type"
const val USER_NAME = "username"

const val  data = "data"
const val  WEB_LINK = "web_Link"



/**
 * 得到AppCompactActivity
 *
 * @param context 上下文
 * @return
 */
fun getAppCompatActivity(context: Context?): AppCompatActivity? {
    if (context == null) {
        return null
    }
    if (context is AppCompatActivity) {
        return context
    } else if (context is ContextThemeWrapper) {
        return getAppCompatActivity(context.baseContext)
    }
    return null
}



/**
 * 初始化
 *
 * @param activity
 */
fun initActionBar(activity: Activity, t: TitleBarColor) {
    if (Build.VERSION.SDK_INT >= 19) {
        if (Build.VERSION.SDK_INT >= 21) {
            val decorView = activity.window.decorView
            when(t){
                TitleBarColor.black -> {
                    val option =
                        View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
                                View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
                    decorView.systemUiVisibility = option
                }
                TitleBarColor.white -> {
                    val option =
                        View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
                                View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    decorView.systemUiVisibility = option
                }
            }
            activity.window.statusBarColor = Color.TRANSPARENT
        } else {
            //透明状态栏
            activity.window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            //透明导航栏
//            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
    }
}




enum class TitleBarColor{
    black,white
}





/**
 * 获得状态栏的高度
 *
 * @param context
 * @return
 */
fun getStatusBarHeight(context: Context): Int {
    var result = 0
    if (Build.VERSION.SDK_INT >= 19) {
        val resourceId = context.resources.getIdentifier(
            "status_bar_height",
            "dimen",
            "android"
        )
        if (resourceId > 0) {
            result = context.resources.getDimensionPixelSize(resourceId)
        }
        return result
    }
    return result
}



fun dipToPx(ctx: Context, dp: Int): Int {
    val density: Float = ctx.resources.displayMetrics.density
    val px = dp * density + 0.5f
    return px.toInt()
}


/**
 * 关闭键盘
 *
 */
fun closeInput(act:Activity){
    val view = act.window.peekDecorView()
    if (view!=null){
        val inputMethodManager = act.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        if(inputMethodManager!=null) {
            inputMethodManager.hideSoftInputFromWindow(view.windowToken,0)
        }
    }
}